package gui_classes;

import java_persistence.PatientMapper;

import java_persistence.UserMapper;

import javax.swing.*;

import org.apache.ibatis.session.SqlSession;

import domain.Patient;

import java.awt.*;
import java.awt.event.*;

/**
 * Is a panel for Adding new patient
 */
public class AddPatientPanel extends RegistrationPanel {

	private JButton btnAdd;
	/**
	 * Create the panel.
	 */
	public AddPatientPanel() {
		super();
		super.removeButton();
		GUI_Main.getFrame().setTitle("Adding A New Patient");
		setPreferredSize(new Dimension(600, 400));
		
		btnAdd = new JButton("Add");
		btnAdd.setBounds(276, 349, 117, 25);
		btnAdd.addActionListener(new AddPatientListener());
		add(btnAdd);
	}
	
	public void display()
	{
		GUI_Main.replacePanel(this);
	}
	
	/**
	 * (non-Javadoc)
	 * @see gui_classes.RegistrationPanel#removeButton()
	 */
	public void removeButton()
	{
		this.remove(btnAdd);
	}
	
	/**(
	 * Listener for Add button
	 */
	private class AddPatientListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			
			String[] myStr = new String[11];
			
			try
			{
				myStr[0] = GUI_Main.addpatientPage.getUsername().getText() ; //username
				myStr[1] = GUI_Main.addpatientPage.getPassword().getText(); //password
				String fName = GUI_Main.addpatientPage.getNameF().getText() + " " + GUI_Main.addpatientPage.getLName().getText(); 
				myStr[2] = fName; //Name
				myStr[3] = GUI_Main.addpatientPage.getPhN().getText(); //phone number
				myStr[4] = GUI_Main.addpatientPage.getAddr().getText(); //address
				myStr[5] = GUI_Main.addpatientPage.getTextField().getText(); //email
				myStr[6] = GUI_Main.addpatientPage.getTxtGender().getText(); // gender
				String bdate = GUI_Main.addpatientPage.getYear().getText() + "-" + GUI_Main.addpatientPage.getMonth().getText() + "-"+GUI_Main.addpatientPage.getDay().getText();
				myStr[7] = bdate; // birthdate
				myStr[8] = GUI_Main.addpatientPage.getTextField1().getText(); //allergies
				myStr[9] = GUI_Main.addpatientPage.getTextField2().getText(); // insurance
				myStr[10] = GUI_Main.addpatientPage.getTextField3().getText(); // pharmacy
				
				GUI_Main.addpatientPage.setArray(myStr);
				System.out.println(myStr[6]);
			
			
				/* -Joseph:  commit new Patient to database ( if there is no problem within registration info 
				 * GUI_Main.addpatientPage.getArray()[0] has username.. and so on.
				 */
				
				Patient new_patient = new Patient();
				new_patient.setUsername(myStr[0]);
				new_patient.setPassword(myStr[1]);
				new_patient.setName(myStr[2]);
				new_patient.setPhone_number(myStr[3]);
				new_patient.setAddress(myStr[4]);
				new_patient.setEmail(myStr[5]);
				new_patient.setIs_male( (myStr[6]=="M")? true: false ); //If the assumption stated above is correct, this construction does work.
				new_patient.setBirthdate(myStr[7]);
				new_patient.setAllergies(myStr[8]);
				new_patient.setInsurance_info(myStr[9]);
				new_patient.setPharmacy(myStr[10]);
				
				SqlSession session = GUI_Main.getSession();
				// changed from UserMapper to PatientMapper
				new_patient.create(session.getMapper(UserMapper.class));
				new_patient.create_patient(session.getMapper(PatientMapper.class));
				
			
				session.commit();
				System.out.println("Added new user to database");
				
				GUI_Main.replacePanel(GUI_Main.lpanel);
			}
			
			catch( Exception e ) {
				System.out.println(e.getMessage());
				JOptionPane.showMessageDialog(null, "Database Error: Please see console/log.");
			}
				
		/*		System.out.println("Added new user to database");
				
				GUI_Main.replacePanel(GUI_Main.frame, GUI_Main.lpanel); 
			}
			catch(NullPointerException e)
			{
				JOptionPane.showMessageDialog(null, "You have not filled in all the data. Try again.");
			} */
				
		}
			
	}

}
